package com.example.imoocmusicdemo.activitys;

import android.content.Context;
import android.content.Intent;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.imoocmusicdemo.R;
import com.example.imoocmusicdemo.helps.DBOpenHelper;
import com.example.imoocmusicdemo.utils.User;
import com.example.imoocmusicdemo.utils.UserUtils;

import java.util.ArrayList;


public class LoginActivity extends BaseActivity {
    TextView tv;
    Button btn,btn2;
    CheckBox cbRmbPsw;
    EditText number,password;
    private CheckBox checkBox;
    private DBOpenHelper mDBOpenHelper;
    private static SharedPreferences.Editor editor;
    private static String userName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        mDBOpenHelper = new DBOpenHelper(this);
        SharedPreferences sp = getSharedPreferences("user_mes", MODE_PRIVATE);

        editor = sp.edit();
        tv = findViewById(R.id.tv);
        btn = findViewById(R.id.btn);
        btn2 = findViewById(R.id.btn2);
        number = findViewById(R.id.number1);
        password = findViewById(R.id.password1);
        cbRmbPsw = findViewById(R.id.cb_rmbPsw);

        checkBox = (CheckBox) findViewById(R.id.CheckBox);

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if(isChecked){
                    //如果选中，显示密码
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    //否则隐藏密码
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        tv.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent("android.intent.action.RegisterActivity");
                startActivityForResult(intent,111);
            }
        });
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String phone = number.getText().toString().trim();
                String pwd = password.getText().toString().trim();
                Log.i("TAG", phone + "_" + pwd);
                if (!UserUtils.validateLogin(LoginActivity.this, phone, pwd, editor, cbRmbPsw,mDBOpenHelper,userName)) {
                    return;
                }
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                intent.putExtra("number",number.getText().toString());
                startActivity(intent);
                finish();
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                intent.putExtra("number", (String) null);
                startActivity(intent);
                finish();
                Toast.makeText(getApplicationContext(),"您当前为游客登录",Toast.LENGTH_LONG).show();
            }
        });
    }
    private  void initView () {
        initNavBar(false,"emoMusic，欢迎您！", false);
    }
}